package cn.iocoder.yudao.module.erp.service.secaccount;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.dept.DeptMapper;
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.yudao.module.erp.controller.admin.secaccount.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.secaccount.SecAccountDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.erp.dal.mysql.secaccount.SecAccountMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;

import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;

/**
 * ERP 二级库台账 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SecAccountServiceImpl implements SecAccountService {

    @Resource
    private SecAccountMapper secAccountMapper;
    @Resource
    private AdminUserMapper adminUserMapper;
    @Resource
    private DeptMapper deptMapper;

    @Override
    public Long createSecAccount(SecAccountSaveReqVO createReqVO) {
        // 插入
        SecAccountDO secAccount = BeanUtils.toBean(createReqVO, SecAccountDO.class);
        secAccountMapper.insert(secAccount);
        // 返回
        return secAccount.getId();
    }

    @Override
    public void updateSecAccount(SecAccountSaveReqVO updateReqVO) {
        // 校验存在
        validateSecAccountExists(updateReqVO.getId());
        // 更新
        SecAccountDO updateObj = BeanUtils.toBean(updateReqVO, SecAccountDO.class);
        secAccountMapper.updateById(updateObj);
    }

    @Override
    public void deleteSecAccount(Long id) {
        // 校验存在
        validateSecAccountExists(id);
        // 删除
        secAccountMapper.deleteById(id);
    }

    private void validateSecAccountExists(Long id) {
        if (secAccountMapper.selectById(id) == null) {
            throw exception(SEC_ACCOUNT_NOT_EXISTS);
        }
    }

    @Override
    public SecAccountDO getSecAccount(Long id) {
        return secAccountMapper.selectById(id);
    }

    @Override
    public PageResult<SecAccountDO> getSecAccountPage(SecAccountPageReqVO pageReqVO) {
        return secAccountMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DeptDO> selectDeptByIds(Set<Long> deptIds) {
        List<DeptDO> deptList = deptMapper.selectBatchIds(deptIds);
        return deptList;
    }

    @Override
    public List<AdminUserDO> selectUserListByIds(Set<String> creatorIds) {
        return adminUserMapper.selectByIds(creatorIds);
    }
}